<template>
  <div class="change-password-container">
    <div class="change-password-card">
      <div class="change-password-header">
        <div class="change-password-logo">AI</div>
        <h1 class="change-password-title">修改密码</h1>
        <p class="change-password-subtitle">请输入当前密码和新密码</p>
      </div>
      
      <form @submit.prevent="handleChangePassword">
        <div class="form-group">
          <label class="form-label">当前密码</label>
          <input 
            type="password" 
            class="form-input" 
            placeholder="请输入当前密码" 
            v-model="passwordForm.currentPassword"
            required
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">新密码</label>
          <input 
            type="password" 
            class="form-input" 
            placeholder="请输入新密码" 
            v-model="passwordForm.newPassword"
            required
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">确认新密码</label>
          <input 
            type="password" 
            class="form-input" 
            placeholder="请再次输入新密码" 
            v-model="passwordForm.confirmPassword"
            required
          >
        </div>
        
        <button type="submit" class="btn btn-primary btn-change-password" :disabled="loading">
          <span v-if="!loading">修改密码</span>
          <span v-else>修改中...</span>
        </button>
        
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <div v-if="successMessage" class="success-message">
          {{ successMessage }}
        </div>
      </form>
      
      <div class="back-to-login">
        <router-link to="/" class="back-link">← 返回登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChangePassword',
  data() {
    return {
      passwordForm: {
        currentPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      loading: false,
      errorMessage: '',
      successMessage: ''
    };
  },
  methods: {
    async handleChangePassword() {
      // 重置消息
      this.errorMessage = '';
      this.successMessage = '';
      
      // 验证密码确认
      if (this.passwordForm.newPassword !== this.passwordForm.confirmPassword) {
        this.errorMessage = '新密码与确认密码不一致';
        return;
      }
      
      // 验证密码长度
      if (this.passwordForm.newPassword.length < 6) {
        this.errorMessage = '新密码长度至少为6位';
        return;
      }
      
      this.loading = true;
      
      try {
        const token = localStorage.getItem('access_token');
        const response = await fetch('http://localhost:8000/api/users/me/password', {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`
          },
          body: JSON.stringify({
            current_password: this.passwordForm.currentPassword,
            new_password: this.passwordForm.newPassword
          })
        });
        
        const data = await response.json();
        
        if (response.ok) {
          // 修改成功
          this.successMessage = '密码修改成功';
          // 清空表单
          this.passwordForm.currentPassword = '';
          this.passwordForm.newPassword = '';
          this.passwordForm.confirmPassword = '';
        } else {
          // 修改失败
          this.errorMessage = data.detail || '密码修改失败，请检查当前密码是否正确';
        }
      } catch (error) {
        console.error('Change password failed:', error);
        this.errorMessage = '网络错误，请稍后重试';
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.change-password-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 0;
}

.change-password-container::before {
  content: '';
  position: absolute;
  width: 300%;
  height: 100%;
  top: auto;
  bottom: 0;
  left: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="white" fill-opacity="0.1" d="M0,128L48,138.7C96,149,192,171,288,170.7C384,171,480,149,576,160C672,171,768,213,864,213.3C960,213,1056,171,1152,149.3C1248,128,1344,128,1392,128L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>') repeat-x bottom;
  background-size: 50% 100%;
  animation: wave 15s infinite linear;
}

@keyframes wave {
  0% { transform: translateX(0); }
  100% { transform: translateX(-25%); }
}

.change-password-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.15);
  width: 100%;
  max-width: 600px;
  padding: 3rem;
  position: relative;
  z-index: 1;
  animation: slideUp 0.5s ease;
}

.change-password-header {
  text-align: center;
  margin-bottom: 2rem;
}

.change-password-logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 1rem;
  background: linear-gradient(135deg, #4A90E2, #764ba2);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 2rem;
  font-weight: bold;
}

.change-password-title {
  font-size: 1.8rem;
  color: #2C3E50;
  margin-bottom: 0.5rem;
}

.change-password-subtitle {
  color: #6c757d;
  font-size: 0.95rem;
}

.btn-change-password {
  width: 100%;
  padding: 0.875rem;
  background: linear-gradient(135deg, #4A90E2, #764ba2);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-change-password:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(74, 144, 226, 0.3);
}

.btn-change-password:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.error-message {
  color: #D9534F;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: rgba(217, 83, 79, 0.1);
  border-radius: 5px;
}

.success-message {
  color: #5CB85C;
  text-align: center;
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: rgba(92, 184, 92, 0.1);
  border-radius: 5px;
}

.back-to-login {
  text-align: center;
  margin-top: 1.5rem;
}

.back-link {
  color: #4A90E2;
  text-decoration: none;
  font-size: 0.9rem;
}

.back-link:hover {
  text-decoration: underline;
}
</style>